package com.mingtu.guards.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.base.BaseFragment;
import com.mingtu.guards.R;
import com.mingtu.center.activity.RecordActivity;
import com.mingtu.center.bean.StatisticsBean;
import com.mingtu.common.callback.StringDialogCallback;
import com.hjq.gson.factory.GsonFactory;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.common.utils.MyLogUtil;
import com.mingtu.common.utils.MyUtills;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.OnClick;

public class Tab3Page extends BaseFragment implements LazyFragmentPagerAdapter.Laziable {


    @BindView(R.id.iv_history_data)
    ImageView ivHistoryData;
    @BindView(R.id.tv_beat)
    TextView tvBeat;
    @BindView(R.id.layout_title)
    LinearLayout layoutTitle;
    @BindView(R.id.tv_grade)
    TextView tvGrade;
    @BindView(R.id.layout_middle)
    RelativeLayout layoutMiddle;
    @BindView(R.id.tv_trask_mileage)
    TextView tvTraskMileage;
    @BindView(R.id.tv_trask_duration)
    TextView tvTraskDuration;
    @BindView(R.id.tv_trask_times)
    TextView tvTraskTimes;
    @BindView(R.id.tv_event_num)
    TextView tvEventNum;
    @BindView(R.id.tv_event_pic)
    TextView tvEventPic;
    @BindView(R.id.tv_event_vioce)
    TextView tvEventVioce;
    @BindView(R.id.tv_event_video)
    TextView tvEventVideo;
    @BindView(R.id.tv_ocr_num)
    TextView tvOcrNum;
    @BindView(R.id.srl_up)
    SmartRefreshLayout srlUp;

    @Override
    protected int getLayoutId() {
        return R.layout.tab3;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView(View view) {
        initListener();
    }

    @Override
    protected void getData() {
        getStatisticsData();
    }

    @OnClick(R.id.iv_history_data)
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.iv_history_data:
                IntentUtils.getInstance().readyGo(getActivity(), RecordActivity.class);
                break;
        }

    }

    private void initListener() {
        initPullRefresh();//刷新
    }

    private void initPullRefresh() {
        srlUp.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
        srlUp.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                srlUp.finishRefresh(Constant.REFRESH_TIME);
                srlUp.setNoMoreData(false);
                srlUp.setEnableLoadMore(false);
                getStatisticsData();
            }
        });
    }


    /**
     * 获取统计数据
     */
    private void getStatisticsData() {
        OkGo.<String>post(Constant.getInstance().POST_STAT)
                .tag(this)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            StatisticsBean bean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                bean = new StatisticsBean();
                                bean = gson.fromJson(result, StatisticsBean.class);

                                StatisticsBean.CurrentWeekBean currentWeekBean = bean.getCurrentWeek();

                                if (currentWeekBean != null) {


                                    String beat = currentWeekBean.getBeat();//百分数
                                    String grade = currentWeekBean.getGrade();//巡护分数
                                    String taskMileage = currentWeekBean.getTaskMileage();// 巡护里程
                                    String taskDuration = currentWeekBean.getTaskDuration();// 巡护时长
                                    String taskNum = currentWeekBean.getTaskNum();// 巡护次数
                                    String eventNum = currentWeekBean.getEventNum();// 上报次数
                                    String picNum = currentWeekBean.getEventPic();// 上图片张数
                                    String voicNum = currentWeekBean.getEventVioce();// 音频个数
                                    String videoNum = currentWeekBean.getEventVideo();// 视频个数
                                    String ocrNum = currentWeekBean.getOcrNum();// 识别个数

                                    if (!StringUtils.isEmpty(beat)) {
                                        tvBeat.setText(beat);
                                    }
                                    if (!StringUtils.isEmpty(grade)) {
                                        tvGrade.setText(grade);
                                    }

                                    if (!StringUtils.isEmpty(taskMileage)) {
                                        tvTraskMileage.setText(taskMileage + "km");
                                    }

                                    if (!StringUtils.isEmpty(taskDuration)) {

                                        long d = 0;
                                        if (taskDuration.indexOf(".") != -1) {
                                            String array[] = taskDuration.split("\\.");
                                            d = Long.parseLong(array[0]);
                                        } else {

                                            d = Long.parseLong(taskDuration);
                                        }
                                        tvTraskDuration.setText(MyUtills.formatSeconds(d));
                                    }
                                    if (!StringUtils.isEmpty(taskNum)) {
                                        tvTraskTimes.setText(taskNum + "次");
                                    }

                                    if (!StringUtils.isEmpty(eventNum)) {
                                        tvEventNum.setText(eventNum + "次");
                                    }
                                    if (!StringUtils.isEmpty(picNum)) {
                                        tvEventPic.setText(picNum + "张");
                                    }
                                    if (!StringUtils.isEmpty(voicNum)) {
                                        tvEventVioce.setText(voicNum + "个");
                                    }
                                    if (!StringUtils.isEmpty(videoNum)) {
                                        tvEventVideo.setText(videoNum + "个");
                                    }
                                    if (!StringUtils.isEmpty(ocrNum)) {
                                        tvOcrNum.setText(ocrNum);
                                    }
                                }
                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }

                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        String result = response.body();
                        MyLogUtil.e(getClass().getSimpleName(), "onError==" + result);

                    }
                });
    }
}